/**
 * Copyright 2010 Andreas Keefer
 *
 * This file is part of jHashMe.
 *
 * jHashMe is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jHashMe is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with jHashMe.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.jhashme.hash.service.internal;

import de.jhashme.hash.service.HashAlgorithm;
import de.jhashme.hash.service.HashAlgorithmNotFoundException;
import de.jhashme.hash.service.HashService;
import de.jhashme.hash.service.HashServiceProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * Implementation of the HashService
 */
public class HashServiceImpl implements HashService {

    private static final Logger LOGGER = LoggerFactory.getLogger(HashServiceImpl.class);

    private Set<HashServiceProvider> hashServiceProviders = new HashSet<HashServiceProvider>();

    /**
     * @see de.jhashme.hash.service.HashService#hash(de.jhashme.hash.service.HashAlgorithm, java.io.File)
     */
    public String hash(HashAlgorithm hashAlgorithm, File fileToHash) throws HashAlgorithmNotFoundException, IOException {
        assert null != hashAlgorithm : "hashAlgorithm must no be null";
        assert null != hashAlgorithm.getName() : "hashAlgorithm.name must not be null";
        assert null != fileToHash : "fileToHash must not be null";
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("calculating '" + hashAlgorithm.getName() + "' hash of file '" + fileToHash.getAbsolutePath() + "'");
        }
        assert fileToHash.exists() : "fileToHash doesn't exist: " + fileToHash.getAbsolutePath();
        assert fileToHash.isFile() : "fileToHash is no File: " + fileToHash.getAbsolutePath();
        assert fileToHash.canRead() : "fileToHash is not Readable: " + fileToHash.getAbsolutePath();

        return getHashServiceProvider(hashAlgorithm).calculateHash(fileToHash);
    }

    /**
     * @see de.jhashme.hash.service.HashService#hash(de.jhashme.hash.service.HashAlgorithm, String)
     */
    public String hash(HashAlgorithm hashAlgorithm, String textToHash) throws HashAlgorithmNotFoundException {
        assert null != hashAlgorithm : "hashAlgorithm must no be null";
        assert null != hashAlgorithm.getName() : "hashAlgorithm.name must not be null";
        assert null != textToHash : "textToHash must not be null";
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("calculating '" + hashAlgorithm.getName() + "' hash of text '" + textToHash + "'");
        }

        return getHashServiceProvider(hashAlgorithm).calculateHash(textToHash);
    }

    private HashServiceProvider getHashServiceProvider(HashAlgorithm hashAlgorithm) throws HashAlgorithmNotFoundException {
        for (HashServiceProvider provider : hashServiceProviders) {
            if (hashAlgorithm.equals(provider.getHashAlgorithm())) {
                return provider;
            }
        }
        throw new HashAlgorithmNotFoundException(hashAlgorithm);
    }

    /**
     * @see de.jhashme.hash.service.HashService#getAvailableHashAlgorithms()
     */
    public Set<HashAlgorithm> getAvailableHashAlgorithms() {
        Set<HashAlgorithm> res = new HashSet<HashAlgorithm>();
        for (HashServiceProvider provider : hashServiceProviders) {
            res.add(provider.getHashAlgorithm());
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("AvailableHashAlgorithms: " + res);
        }
        return res;
    }

    public void setHashServiceProviders(Set<HashServiceProvider> hashServiceProviders) {
        this.hashServiceProviders = hashServiceProviders;
    }
}
